package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;
import com.lb.jwitter.service.TblQLDTTXALichKiemTraGK;
import com.lb.jwitter.service.TblQLDTTXALichKiemTraGKService;
import com.lb.jwitter.service.TblQLDTTXALichThi;
import com.lb.jwitter.service.TblQLDTTXALichThiService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVien;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class TblQLDTTXALichThiHocVienSearchAction {
	private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }
    
    private int view;
    
    private List<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    public List<TblQLDTTXAHocVien>  getTblQLDTTXAHocViens(){ 
              return tblQLDTTXAHocViens; 
     } 
    
    private TblQLDTTXAHocVien tblQLDTTXAHocVien;
    private String mSHV;

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 


    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public List<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams(){ 
              return tblQLDTTXAHocKyTrongNams; 
     } 

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

        
    private TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam;
    
    private TblQLDTTXALopMonHocHocVienService tblQLDTTXALopMonHocHocVienService;
    private List<TblQLDTTXALopMonHocHocVien> tblQLDTTXALopMonHocHocViens;
    
    private Long hocKyId;

    private List<TblQLDTTXALichThi> tblQLDTTXALichThis;
    
    private TblQLDTTXALichThiService tblQLDTTXALichThiService;
    
    private List<TblQLDTTXALichKiemTraGK> tblQLDTTXALichKiemTraGKs;
    
    private TblQLDTTXALichKiemTraGKService tblQLDTTXALichKiemTraGKService;
    
    

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LichThi", "xem")
    		|| !tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LichKiemTraGK", "xem")){ 
        return "noprivilege"; 
    }
    
        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService) context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient");
        
        tblQLDTTXALichKiemTraGKService = (TblQLDTTXALichKiemTraGKService) context.getBean("tbl_QLDTTXA_LichKiemTraGKClient");
        
        tblQLDTTXALichThiService = (TblQLDTTXALichThiService) context.getBean("tbl_QLDTTXA_LichThiClient");
        
        tblQLDTTXALichKiemTraGKs = new ArrayList<TblQLDTTXALichKiemTraGK>();
        
        tblQLDTTXALichThis = new ArrayList<TblQLDTTXALichThi>();
        
       if(pageSize == 0){ 
            pageSize = 20; 
        } 
        if(page == 0){ 
            page =1; 
        } 
        
        //get hocky
        Date today = new Date();
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        if(hocKyId != null && !hocKyId.equals(new Long(0))){
        	ConditionReport con = new ConditionReport(); 
            con.setCol("id");
            con.setValue1(hocKyId);
            con.setOperator("eq");
            cons.add(con);	
        } else{
        	DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 

        	ConditionReport con = new ConditionReport(); 
            con.setCol("thoiGianBatDau");
            con.setValue1(df.format(today));
            con.setOperator("le");
            con.setType("date");
            cons.add(con);	
            
            ConditionReport con2 = new ConditionReport(); 
            con2.setCol("thoiGianKetThuc");
            con2.setValue1(df.format(today));
            con2.setOperator("ge");
            con2.setType("date");
            cons.add(con2);	
        }
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);

        if(tblQLDTTXAHocKyTrongNams != null && tblQLDTTXAHocKyTrongNams.size() ==1){ 
            tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNams.get(0);
        }

        //Get hocVien
        cons.clear();
        if(mSHV != null && !mSHV.equalsIgnoreCase("")){
            ConditionReport con = new ConditionReport(); 
            con.setCol("MSHV");
            con.setValue1(mSHV);
            con.setOperator("eq");
            cons.add(con);	

        }else{
        	ConditionReport con = new ConditionReport(); 
            con.setCol("MSHV");
            con.setValue1("");
            con.setOperator("eq");
            cons.add(con);	
        }
        
        tblQLDTTXAHocViens = tblQLDTTXAHocVienService.findtblQLDTTXAHocVienByCondition(cons, 0, 0);
        if(tblQLDTTXAHocViens != null && tblQLDTTXAHocViens.size() > 0)
        	tblQLDTTXAHocVien = tblQLDTTXAHocViens.get(0);
        
        //LopHoc cua HocVien
        cons.clear();
        if(tblQLDTTXAHocVien != null){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.id");
            con.setValue1(tblQLDTTXAHocVien.getId());
            con.setOperator("eq");
            cons.add(con);	

        }else{
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.id");
            con.setValue1(new Long(0));
            con.setOperator("eq");
            cons.add(con);	
        }
        if(tblQLDTTXAHocKyTrongNam != null){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("lopMonHocId.hocKyId.id");
            con.setValue1(tblQLDTTXAHocKyTrongNam.getId());
            con.setOperator("eq");
            cons.add(con);	

        }else{
        	ConditionReport con = new ConditionReport(); 
            con.setCol("lopMonHocId.hocKyId.id");
            con.setValue1("");
            con.setOperator("eq");
            cons.add(con);	
        }
        tblQLDTTXALopMonHocHocViens = tblQLDTTXALopMonHocHocVienService.findtblQLDTTXALopMonHocHocVienByCondition(cons, 0, 0);
        
        for(TblQLDTTXALopMonHocHocVien tblQLDTTXALopMonHocHocVien: tblQLDTTXALopMonHocHocViens){
        	cons.clear();
            if(true){ 
                ConditionReport con = new ConditionReport(); 
                con.setCol("lopId.id");
                con.setValue1(tblQLDTTXALopMonHocHocVien.getLopMonHocId().getId());
                con.setOperator("eq");
                cons.add(con);
            }
            if(tblQLDTTXALopMonHocHocVien.getNhomId() != null){ 
                ConditionReport con = new ConditionReport(); 
                con.setCol("nhomId.id");
                con.setValue1(tblQLDTTXALopMonHocHocVien.getNhomId().getId());
                con.setOperator("eq");
                cons.add(con);
            }else{
            	ConditionReport con = new ConditionReport(); 
                con.setCol("nhomId.id");
                con.setValue1(new Long(1));
                con.setOperator("eq");
                cons.add(con);
            }
            List<TblQLDTTXALichKiemTraGK> lichKiemTras = tblQLDTTXALichKiemTraGKService.findtblQLDTTXALichKiemTraGKByCondition(cons, 0, 0);
            tblQLDTTXALichKiemTraGKs.addAll(lichKiemTras);
            
            List<TblQLDTTXALichThi> lichThis = tblQLDTTXALichThiService.findtblQLDTTXALichThiByCondition(cons, 0, 0);
            tblQLDTTXALichThis.addAll(lichThis);
        }
        
        
        
      
      
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Search tbl_QLDTTXA_LichThiHocVien Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 }

public TblQLDTTXAHocKyTrongNam getTblQLDTTXAHocKyTrongNam() {
	return tblQLDTTXAHocKyTrongNam;
}

public void setTblQLDTTXAHocKyTrongNam(
		TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam) {
	this.tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNam;
}

public Long getHocKyId() {
	return hocKyId;
}

public void setHocKyId(Long hocKyId) {
	this.hocKyId = hocKyId;
}


public int getView() {
	return view;
}

public TblQLDTTXAHocVien getTblQLDTTXAHocVien() {
	return tblQLDTTXAHocVien;
}

public void setTblQLDTTXAHocVien(TblQLDTTXAHocVien tblQLDTTXAHocVien) {
	this.tblQLDTTXAHocVien = tblQLDTTXAHocVien;
}

public String getMSHV() {
	return mSHV;
}

public void setMSHV(String mshv) {
	mSHV = mshv;
}

public List<TblQLDTTXALopMonHocHocVien> getTblQLDTTXALopMonHocHocViens() {
	return tblQLDTTXALopMonHocHocViens;
}

public void setTblQLDTTXALopMonHocHocViens(
		List<TblQLDTTXALopMonHocHocVien> tblQLDTTXALopMonHocHocViens) {
	this.tblQLDTTXALopMonHocHocViens = tblQLDTTXALopMonHocHocViens;
}

public void setTblQLDTTXAHocViens(List<TblQLDTTXAHocVien> tblQLDTTXAHocViens) {
	this.tblQLDTTXAHocViens = tblQLDTTXAHocViens;
}


public void setTblQLDTTXAHocKyTrongNams(
		List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams) {
	this.tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNams;
}


public void setView(int view) {
	this.view = view;
} 

public List<TblQLDTTXALichThi> getTblQLDTTXALichThis(){
	return tblQLDTTXALichThis;
}

public List<TblQLDTTXALichKiemTraGK> getTblQLDTTXALichKiemTraGKs(){
	return tblQLDTTXALichKiemTraGKs;
}

}
